package org.example.linkedList;

/**
 * @Auther: wangbw
 * @Date:2020/12/22
 * @Description: org.example.linkedList
 * @version: 1.0
 */
public class RadixSort {
    public static void sort(int[] number, int d) //d表示最大的数有多少位
    {
        if (number == null || number.length <=1)
            return;
        int n = 1;
        for (int i = 0; i < d; i++) {
            int[][] buckets = new int[10][number.length];
            int[] indexArr = new int[10];
            for (int j = 0; j < number.length; j++) {
                int index = number[j] / n % 10;
                buckets[index][indexArr[index]++] = number[j];
            }
            int numIndex = 0;
            for (int j = 0; j < buckets.length; j++) {
                if (indexArr[j] == 0)
                    continue;
                for (int k = 0; k < indexArr[j]; k++) {
                    number[numIndex++] = buckets[j][k];
                }
            }
            n *= 10;
        }
    }

    public static void main(String[] args) {
        int[] data = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81, 33, 100};
        RadixSort.sort(data, 3);
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + "\t");
        }
    }
}
